package com.sensePlusVR.buriedpoint;

import com.alibaba.fastjson.JSON;
import com.sensePlusVR.bean.BuriePointEventReqDto;
import com.sensePlusVR.bean.BuriedPointDLData;
import com.sensePlusVR.bean.CollectionItem;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.ConstantURL;
import com.sensePlusVR.utils.DateTimeUtil;
import com.sensePlusVR.utils.DownloadTask;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.UserUtil;

public class GameDownloadBP {
	private static final String TAG = GameDownloadBP.class.getSimpleName();
	
	private static GameDownloadBP INSTANTS;
	
	static public GameDownloadBP getInstants(){
		if (INSTANTS == null){
			INSTANTS = new GameDownloadBP();
		}
		return INSTANTS;
	}
	
	/**
	 * 设置添加下载埋点
	 */
	public void saveDownloadAddBuried(GameItem gameItem){
		
		MyLog.d(TAG, "saveDownloadAddBuried gameId : " + gameItem.getId());
		
		CollectionItem clcItem = CollectionItem.query(gameItem.getId());
		
		BuriePointEventReqDto buriePointEventReqDto = null;
		
		if(clcItem != null){
			
			int downloadState = clcItem.getDownloadState();
			
			//不会出现DownlodDing,然后只有当DownloadError和DownloadPause则新建条目
			if (downloadState == CollectionItem.DownloadError
					|| downloadState == CollectionItem.DownloadPause) {
				
				MyLog.d(TAG, "saveDownloadAddBuried update item");

				return;

			}
			
		}
			
		MyLog.d(TAG, "saveDownloadAddBuried new item");
		
		buriePointEventReqDto = new BuriePointEventReqDto();
			
		
		buriePointEventReqDto.setUid(UserUtil.getUserId());
		
		buriePointEventReqDto.setEvent(Constant.BURIED_GAME_DOWNLOAD_ADD);
		
		BuriedPointDLData buriedPointDLData = new BuriedPointDLData();
		buriedPointDLData.setGame_id(gameItem.getId());
		buriedPointDLData.setTitle(gameItem.getTitle());
		buriedPointDLData.setActors(gameItem.getActors());
		buriedPointDLData.setFormat(gameItem.getFormat());
		buriedPointDLData.setTags(gameItem.getTags());
		buriedPointDLData.setVersion(gameItem.getVersion());
		buriedPointDLData.setType(gameItem.getType());
		buriedPointDLData.setGrade(gameItem.getGrade());
		buriedPointDLData.setCompany(gameItem.getCompany());
		buriedPointDLData.setArea(gameItem.getArea());
		buriedPointDLData.setIsfree(gameItem.getIsfree());
		buriedPointDLData.setRating(gameItem.getRating());
		buriedPointDLData.setLevel(gameItem.getLevel());
		buriedPointDLData.setDirectors(gameItem.getDirectors());
		
		
		buriePointEventReqDto.setData(JSON.toJSONString(buriedPointDLData));
		
		long timeL = System.currentTimeMillis();
		String timeStr = DateTimeUtil.getStandTime(timeL);
		buriePointEventReqDto.setEvent_time(timeStr);
		
		MyLog.d(TAG, "saveDownloadAddBuried event_time : " + timeStr);
		
		buriePointEventReqDto.setStart_time(timeL);
		
		buriePointEventReqDto.setMode(ConstantURL.BURIED_MODE);
		
		buriePointEventReqDto.setClcItemId(clcItem == null?0:clcItem.get_id());
		
		long startTimeL = buriePointEventReqDto.getStart_time();
		long endTimeL = System.currentTimeMillis();
		long duration = endTimeL - startTimeL;
		
		buriePointEventReqDto.setDuration(duration);
		
		buriePointEventReqDto.setEnd_time(endTimeL);
		
		buriePointEventReqDto.setFlag(Constant.BURIED_FLAG_SUCCESS);
		buriePointEventReqDto.setState(Constant.BURIED_STATE_SUCCESS);
		
		BuriePointEventReqDto.saveOrUpdate(buriePointEventReqDto);
	}
	
	/**
	 * @param gameItem
	 * @param downloadState
	 * 设置下载到下载成功埋点
	 */
	public void saveGameDownloadBuried(GameItem gameItem,int downloadState){
		//设置埋点初始值
		boolean isNotUpdate = true;
		
		CollectionItem clcItem = CollectionItem.query(gameItem.getId());
		
		BuriePointEventReqDto buriePointEventReqDto = null;
		
		if(clcItem != null){
			
			buriePointEventReqDto = BuriePointEventReqDto.getBuriePointDownloadByClcItemId(clcItem.get_id());
			
			if(buriePointEventReqDto != null){
				
				MyLog.d(TAG, "saveGameDownloadBuried buriePointEventReqDto : " + buriePointEventReqDto.toString());
				
				Integer state = buriePointEventReqDto.getState();
				
				if(state != null){
					
					if (state != Constant.BURIED_STATE_SUCCESS) {
						
						isNotUpdate = false;
						
					}	
				}else{
					
					isNotUpdate = false;
					
				}
				
			}else{
				
				MyLog.d(TAG, "saveGameDownloadBuried buriePointEventReqDto == null");
				
			}
		}
		
		MyLog.d(TAG, "saveGameDownloadBuried isNotUpdate : " + isNotUpdate);
		
		if(isNotUpdate){
			
			buriePointEventReqDto = new BuriePointEventReqDto();
			
		}
		
		
		if(downloadState == DownloadTask.DownloadRealDoing){
			
			buriePointEventReqDto.setUid(UserUtil.getUserId());
			
			buriePointEventReqDto.setEvent(Constant.BURIED_GAME_DOWNLOAD);
			
			BuriedPointDLData buriedPointDLData = new BuriedPointDLData();
			buriedPointDLData.setGame_id(gameItem.getId());
			buriedPointDLData.setTitle(gameItem.getTitle());
			buriedPointDLData.setActors(gameItem.getActors());
			buriedPointDLData.setFormat(gameItem.getFormat());
			buriedPointDLData.setTags(gameItem.getTags());
			buriedPointDLData.setVersion(gameItem.getVersion());
			buriedPointDLData.setType(gameItem.getType());
			buriedPointDLData.setGrade(gameItem.getGrade());
			buriedPointDLData.setCompany(gameItem.getCompany());
			buriedPointDLData.setArea(gameItem.getArea());
			buriedPointDLData.setIsfree(gameItem.getIsfree());
			buriedPointDLData.setRating(gameItem.getRating());
			buriedPointDLData.setLevel(gameItem.getLevel());
			buriedPointDLData.setDirectors(gameItem.getDirectors());
			
			
			buriePointEventReqDto.setData(JSON.toJSONString(buriedPointDLData));
			
			long timeL = System.currentTimeMillis();
			String timeStr = DateTimeUtil.getStandTime(timeL);
			buriePointEventReqDto.setEvent_time(timeStr);
			
			buriePointEventReqDto.setStart_time(timeL);
			
			buriePointEventReqDto.setMode(ConstantURL.BURIED_MODE);
			
			buriePointEventReqDto.setClcItemId(clcItem == null?0:clcItem.get_id());
			
			buriePointEventReqDto.setState(Constant.BURIED_STATE_NOMAL);
			
			BuriePointEventReqDto.saveOrUpdate(buriePointEventReqDto);
			
			
			
		}else if(downloadState == DownloadTask.DownloadSuccess){
			
			Long startTimeLL = buriePointEventReqDto.getStart_time();
			long startTimeL = System.currentTimeMillis();
			if(startTimeLL != null){
				startTimeL = startTimeLL;
			}
			long endTimeL = System.currentTimeMillis();
			long duration = endTimeL - startTimeL;
			
			buriePointEventReqDto.setDuration(duration);
			
			buriePointEventReqDto.setEnd_time(endTimeL);
			
			buriePointEventReqDto.setFlag(Constant.BURIED_FLAG_SUCCESS);
			buriePointEventReqDto.setState(Constant.BURIED_STATE_SUCCESS);
			
			BuriePointEventReqDto.saveOrUpdate(buriePointEventReqDto);
		}
	}

}
